﻿package mortal.game.view.CollectExchange.panel
{
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import extend.language.*;
    import fl.data.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.CollectExchange.*;
    import mortal.game.view.CollectExchange.cellRender.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.signUp.view.*;

    public class CollectExchangePanel extends SignUpViewBase
    {
        private var _collectionGiftList:GTileList;
        private var _selectedGiftIndex:int;
        private var _isCreateChildren:Boolean;
        protected var _min:BitmapNumberText;
        protected var _day:BitmapNumberText;
        protected var _hour:BitmapNumberText;
        protected var _secView:SecTimerView;

        public function CollectExchangePanel()
        {
            return;
        }// end function

        private function onResLoadCompl() : void
        {
            if (isDisposed)
            {
                return;
            }
            this.createChildren();
            return;
        }// end function

        private function createChildren() : void
        {
            this._isCreateChildren = true;
            this.pushUIToDisposeVec(UIFactory.bg(2, 4, 686, 182, this, ImagesConst.collectExchangeBg));
            this.pushUIToDisposeVec(UIFactory.bg(0, 189, 690, 358, this, ImagesConst.PanelBg));
            this._day = UIFactory.bitmapNumberText(152 + 80 - 2, 143 - 60 - 2, "investmentNum.png", 24, 30, -8, this, 9, 1);
            this._day.text = "0";
            this._hour = UIFactory.bitmapNumberText(205 + 80 + 2, 143 - 60 - 2, "investmentNum.png", 24, 30, -8, this, 9, 1);
            this._hour.text = "0";
            this._min = UIFactory.bitmapNumberText(276 + 80 + 2, 143 - 60 - 2, "investmentNum.png", 24, 30, -8, this, 9, 1);
            this._min.text = "0";
            this._secView = new SecTimerView();
            this._secView.addEventListener(EventName.SecViewTimeChange, this.onTimeChangeHd);
            this.setTimeCountDown();
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(90002), 30, 130, 600, 200, this, GlobalStyle.textFormatBai.setLeading(5).setSize(14), true));
            this._collectionGiftList = UIFactory.tileList(0, 192, 670 + 20, 360, this);
            this._collectionGiftList.rowHeight = 87;
            this._collectionGiftList.columnWidth = 688;
            this._collectionGiftList.horizontalGap = 0;
            this._collectionGiftList.verticalGap = 0;
            this._collectionGiftList.setStyle("cellRenderer", CollectExchangeCellRender);
            this.initGiftList();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.collectExchange, this.onResLoadCompl);
            return;
        }// end function

        private function initGiftList() : void
        {
            this._collectionGiftList.dataProvider = this.getCollectionGiftListDataProvider();
            this._collectionGiftList.drawNow();
            this._collectionGiftList.verticalScrollBar.x = 675;
            this._collectionGiftList.scrollToIndex(this._selectedGiftIndex);
            return;
        }// end function

        private function getCollectionGiftListDataProvider() : DataProvider
        {
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.collectReward.exchangeGroups;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                if (CollectExchangeUtil.isNormalRecipe(_loc_2[_loc_3]))
                {
                    _loc_1.addItem(_loc_2[_loc_3]);
                    if (this._selectedGiftIndex <= 0)
                    {
                        if (_loc_2[_loc_3].isSatisfyRecipe())
                        {
                            this._selectedGiftIndex = _loc_3;
                        }
                    }
                }
                _loc_3++;
            }
            return _loc_1;
        }// end function

        public function refresh(param1:Boolean = false) : void
        {
            if (this._isCreateChildren)
            {
                this._collectionGiftList.dataProvider = this.getCollectionGiftListDataProvider();
                this._collectionGiftList.drawNow();
                this._collectionGiftList.verticalScrollBar.x = 675;
                this._collectionGiftList.scrollToIndex(this._selectedGiftIndex);
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            LoaderHelp.removeResEvent(ResFileConst.collectExchange, this.onResLoadCompl);
            if (!this._isCreateChildren)
            {
                return;
            }
            this._isCreateChildren = false;
            this._collectionGiftList.dispose(param1);
            this._collectionGiftList = null;
            this._selectedGiftIndex = 0;
            this._secView.removeEventListener(EventName.SecViewTimeChange, this.onTimeChangeHd);
            this._secView.stop();
            this._secView.dispose();
            this._secView = null;
            this._day.dispose(param1);
            this._hour.dispose(param1);
            this._min.dispose(param1);
            this._day = null;
            this._hour = null;
            this._min = null;
            return;
        }// end function

        protected function setTimeCountDown() : void
        {
            var _loc_1:* = this.getLeftTime();
            this._secView.stop();
            if (_loc_1 < 0)
            {
                return;
            }
            this._secView.setLeftTime(_loc_1);
            return;
        }// end function

        protected function getLeftTime() : Number
        {
            return CollectExchangeUtil.getLeftTime() / 1000;
        }// end function

        protected function onTimeChangeHd(event:DataEvent) : void
        {
            if (this._secView == null)
            {
                return;
            }
            if (this._secView.secTimerViewData == null)
            {
                this._secView.stop();
                return;
            }
            var _loc_2:* = this._secView.secTimerViewData.dd();
            var _loc_3:* = this._secView.secTimerViewData.hh();
            var _loc_4:* = this._secView.secTimerViewData.mm();
            if (_loc_2.length == 2 && _loc_2 == "0")
            {
                _loc_2 = _loc_2.substring(1);
            }
            this._day.text = "" + _loc_2;
            this._hour.text = "" + _loc_3;
            this._min.text = "" + _loc_4;
            return;
        }// end function

    }
}
